package org.warehouse.dao.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.warehouse.dao.AbstractDao;
import org.warehouse.dao.ICategoryDao;
import org.warehouse.dao.IItemDao;
import org.warehouse.dao.engine.DataObject;
import org.warehouse.dao.engine.IDbExecutor;
import org.warehouse.entity.Category;
import org.warehouse.entity.Item;

import com.google.common.collect.ImmutableList;

@Service
public class CategoryDao extends AbstractDao<Category> implements ICategoryDao {

    @Autowired
    private IDbExecutor dbExecutor;
    
    @Autowired
    private IItemDao itemDao;
    
    public CategoryDao() {
		super(Category.class);
	}

	@Override
    public List<Item> getItems(Integer categoryId) {
        String request = MessageFormat.format("SELECT * FROM {0} WHERE {1} = ?", itemDao.getTableName(), "category_id");
        SqlRowSet rowSet = dbExecutor.invoke(request, ImmutableList.<DataObject<?>>of(DataObject.of(categoryId)));

        List<Item> entities = new ArrayList<Item>();
        while (rowSet.next()) {
            entities.add(itemDao.createAndFillEntity(rowSet));
        }

        return entities;
    }

}
